package servlet;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login.do")
public class ServletLogin extends HttpServlet {
	private  Properties mapa;
	
    public void init() throws ServletException{
    	try{
    		//instanciando a tabela Properties...
    		this.mapa = new Properties();
    		
    		//obtendo os parametros do web.xml
    		String caminho = this.getServletContext().getInitParameter("caminho");
    		
    		//populando a tabela Propities com os arquivos de senhas ...
    		this.mapa.load(this.getServletContext().getResourceAsStream(caminho));
    	}catch (IOException e)
    	{
    		System.out.println("Erro no preenchimento");
    		e.printStackTrace();
    	}
    }
	
    public ServletLogin() {
        super();
        
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("edtLogin");
		String senha = request.getParameter("edtSenha");
		
		if (validarSenha(login, senha))
			response.sendRedirect("loja.html");
		else
			response.sendRedirect("erro.html");
	}

	private boolean validarSenha (String login, String senha){
		return this.mapa.getProperty(login).equals(senha);
	}
}
